package com.foolishbird.sdk.ui.fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.params.FloatKefuResponseParams;
import com.foolishbird.sdk.params.RedBindedResponseParams;
import com.foolishbird.sdk.params.RedWechatResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.bean.RedBindedInfo;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.utils.MyFragmentManger;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.WL;

import java.util.List;


public class RedpackageWeChatFragment extends BaseHttpFragment implements View.OnClickListener {



    private TextView titleTxt;

    private ImageView backView;
    WebView mWebView;
    TextView codeText;
    private Button confirmBtn;

    public static final RedpackageWeChatFragment newInstance(Bundle bundle) {
        RedpackageWeChatFragment fragment = new RedpackageWeChatFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "fb_redpackage_wechat");
        View contentView =inflater.inflate(resource,null);
        backView = contentView.findViewById(getViewIdentifier("fb_fragment_login_back"));
        backView.setOnClickListener(this);
        titleTxt = contentView.findViewById(getViewIdentifier("fb_fragment_float_title"));
        titleTxt.setText("微信账号绑定");
        mWebView = contentView.findViewById(getViewIdentifier("redpackage_wechat_webview"));
        mWebView.setBackgroundColor(0);
        mWebView.getBackground().setAlpha(0);
        mWebView.setWebViewClient(new WebViewClient(){
                                         @Override
                                         public void onPageFinished(WebView view, String url) {
                                             super.onPageFinished(view, url);
                                             view.scrollTo(0,0);
                                         }
                                     }
        );
        codeText = contentView.findViewById(getViewIdentifier("redpackage_wechat_code"));
        confirmBtn = contentView.findViewById(getViewIdentifier("redpakcage_alipay_confirm"));
        confirmBtn.setOnClickListener(this);
        HttpEngineApi.getInstance().redpackageWechat(requestCallbackHandler(RedWechatResponseParams.class));
        return contentView;

    }

    private List<RedBindedInfo> myNoticeList;

    @Override
    public void onAsyncRequestResult(int flag, ResponseParams response, Object... params) {
        super.onAsyncRequestResult(flag, response, params);
        if (response != null) {
            int code = response.getState().getCode();
            if (State.SUCCESS == code) {
                if(response instanceof  RedWechatResponseParams){
                    RedWechatResponseParams.RedWechatResponseData data = ((RedWechatResponseParams)response).getData();
                    mWebView.loadDataWithBaseURL(null, data.getContent(), "text/html", "utf-8", null);
                    codeText.setText(data.getGameCode());
                }
            }
        }



        WL.d("FloatNoticeFragment",response.toString());
    }

    private int deleteTaskId;





    @Override
    public void onClick(View view) {
        if(view ==backView){
            MyFragmentManger.getInstance().goBack(this);
        }else if(view == confirmBtn){
            //获取剪贴板管理器：
            ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", codeText.getText().toString().trim());
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            Toast.makeText(getContext(),"复制成功",Toast.LENGTH_SHORT).show();
        }
//        OnFinishActivityListener listener = getOnFinishActivityListener();
//        if (listener != null) {
//            listener.doFinishActivity(this);
//        }
    }


}
